package cn.wolfcode.mp.service.impl;

import cn.wolfcode.mp.anno.RequiredPermission;
import cn.wolfcode.mp.domain.Permission;
import cn.wolfcode.mp.mapper.PermissionMapper;
import cn.wolfcode.mp.query.PermissionQuery;
import cn.wolfcode.mp.service.IPermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;


@Service
@Transactional // 事务
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public IPage<Permission> queryPage(PermissionQuery qo) {
        IPage<Permission> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        //条件查询
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        //qo需要进行判空,否则查询不出来,查出来的条件为null
        if (qo.getKeyword() != null && !"".equals(qo.getKeyword())) {
            wrapper.like("name", qo.getKeyword());
        }
        return super.page(page, wrapper);
    }

    @Autowired
    private ApplicationContext ctx;

    @Override
    public void reload() {
        //查询所有的权限表达式
        List<String> expressions = permissionMapper.selectAllExpression();
        //获取每个控制器对象
        // 方式一: Class<DepartmentController> aClass = DepartmentController.class;
        //方式二: 从spring容器中获取带有Controller注解的bean,获取每个控制器对象
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
        //获取所有控制器对象
        Collection<Object> controllers = map.values();
        for (Object controller : controllers) {
            Class<?> aClass = controller.getClass();
            //获取所有本类方法
            Method[] methods = aClass.getDeclaredMethods();
            for (Method method : methods) {
                //获取方法上自定义的权限注解
                RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
                //判断是否有贴注解
                if (annotation == null) {
                    continue; //跳过
                }
                //从注解中获取权限名和权限表达式
                String expression = annotation.expression();
                String name = annotation.name();
                //判断数据库是否已经存在该权限表达式
                if (expressions.contains(expression)) {
                    continue; //跳过
                }
                //把数据封装为权限对象
                Permission permission = new Permission();
                permission.setName(name);
                permission.setExpression(expression);
                //插入到数据库
                permissionMapper.insert(permission);
                //重新加回去
                expressions.add(expression);
            }
        }
    }

    @Override
    public List<String> selectByEmpId(Long id) {
        return permissionMapper.selectByEmpId(id);
    }
}